--original found here: http://www.garrysmod.org/downloads/?a=view&id=118302
--rewrote the vast majority of it
 
SWEP.Author = "BlackRaven"
SWEP.Instructions = "Left click to crack a computer terminal"
SWEP.Contact = ""
SWEP.Purpose = ""

SWEP.ViewModelFOV = 62
SWEP.ViewModelFlip = false
SWEP.ViewModel = Model("models/weapons/v_c4.mdl")
SWEP.WorldModel = Model("models/weapons/w_c4.mdl")

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.Sound = Sound("weapons/deagle/deagle-1.wav")

SWEP.Primary.ClipSize = -1      -- Size of a clip
SWEP.Primary.DefaultClip = 0        -- Default number of bullets in a clip
SWEP.Primary.Automatic = false      -- Automatic/Semi Auto
SWEP.Primary.Ammo = ""

SWEP.Secondary.ClipSize = -1        -- Size of a clip
SWEP.Secondary.DefaultClip = -1     -- Default number of bullets in a clip
SWEP.Secondary.Automatic = false        -- Automatic/Semi Auto
SWEP.Secondary.Ammo = ""

SWEP.KeyCrackTime = 5 --seconds
SWEP.EntToCrack = "ent_pi_factory_lootrefine"
SWEP.CrackDistance = 200 --the fartest you can be away to crack

/*---------------------------------------------------------
Name: SWEP:PrimaryAttack()
Desc: +attack1 has been pressed
---------------------------------------------------------*/

/*
TODO: add hud items
*/

function SWEP:PrimaryAttack()
	self.Weapon:SetNextPrimaryFire(CurTime() + .4)
	if self.IsCracking then return end

	local trace = self.Owner:GetEyeTrace()
	local e = trace.Entity
	
	
	if(trace.Entity:GetClass() == self.EntToCrack)then
		
		local YourTeam = self.Owner:Team()
			
		if ValidEntity(e) and trace.HitPos:Distance(self.Owner:GetShootPos()) <= self.CrackDistance and (e:GetClass() == self.EntToCrack) and (tonumber(e:GetTeam()) != tonumber(YourTeam)) then
			self.IsCracking = true
			self.StartCrack = CurTime()
			self.EndCrack	= CurTime() + self.KeyCrackTime
			
			self:SetWeaponHoldType("pistol")
			timer.Create("KeyCrackSounds", 1, self.KeyCrackTime, function(wep)
				wep:EmitSound("buttons/blip2.wav", 100, 100)
			end, self)			
		end 
	end
end

function SWEP:Holster()
	self.IsCracking = false
	timer.Destroy("KeyCrackSounds")
	return true
end

function SWEP:Succeed()
	self.IsCracking = false
	local trace = self.Owner:GetEyeTrace()
	
	if ValidEntity(trace.Entity) and trace.Entity:GetClass() == self.EntToCrack then
	
		//local owner = trace.Entity:GetNWEntity("factory_owner")
		trace.Entity:Hacked(self.Owner) --runs function in "ent_pi_factory_lootrefine/init.lua"
		
		trace.Entity:EmitSound("buttons/button11.wav")	
	end
	timer.Destroy("KeyCrackSounds")
end

function SWEP:Fail()
	self.IsCracking	= false
	self:SetWeaponHoldType("normal")
	timer.Destroy("KeyCrackSounds")
end

function SWEP:Think()
	if self.IsCracking then
		local trace = self.Owner:GetEyeTrace()
		if not ValidEntity(trace.Entity) then 
			self:Fail()
		end
		if trace.HitPos:Distance(self.Owner:GetShootPos()) > self.CrackDistance or (trace.Entity:GetClass() != self.EntToCrack) then
			self:Fail()
		end
		if self.EndCrack <= CurTime() then
			self:Succeed()
		end
	end
end

function SWEP:SecondaryAttack()
	self:PrimaryAttack()
end